﻿<Window x:Class="EAL.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:EAL.ViewModels"
        xmlns:mvvm="clr-namespace:EAL.MVVM"
        xmlns:local="clr-namespace:EAL"
        xmlns:db="clr-namespace:EAL.Database"
        xmlns:models="clr-namespace:EAL.Models"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:converters="clr-namespace:EAL.Converters"
        Title="EAL" Height="500" Width="800" WindowStartupLocation="CenterScreen" > 
    <Window.Resources>
        <!--ViewModel for Products-Tab-->
        <vm:ProductsVM DbContext="{StaticResource DbContext}" x:Key="ProductsVM" />
        <!--ViewModel for Costumers-Tab-->
        <vm:CustomersVM DbContext="{StaticResource DbContext}" x:Key="CustomersVM" />
        <!--ViewModel for Orders-Tab-->
        <vm:OrdersVM DbContext="{StaticResource DbContext}" x:Key="OrdersVM" />
        <!--override the default opacity for disabled buttons-->
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Opacity" Value="0.5" />
                </Trigger>
            </Style.Triggers>
        </Style> 
    </Window.Resources>
    
    <Grid>
        <Menu>
            <MenuItem Header="Datei">
                <MenuItem Name="import" Command="{Binding ElementName=MainTabControl, Path=SelectedItem.DataContext.ImportCommand}" Header="{Binding ElementName=MainTabControl, Path=SelectedItem.DataContext.ImportCommandDescription}">
                    <MenuItem.Icon>
                        <Image Width="20" Height="20" Source="images/import.ico" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Name="export" Command="{Binding ElementName=MainTabControl, Path=SelectedItem.DataContext.ExportCommand}" Header="{Binding ElementName=MainTabControl, Path=SelectedItem.DataContext.ExportCommandDescription}">
                    <MenuItem.Icon>
                        <Image Width="20" Height="20" Source="images/export.ico" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="Hilfe">
                <MenuItem Header="Info" Click="MenuItem_Click_1">
                    <MenuItem.Icon>
                        <Image Width="20" Height="20" Source="images/info.ico" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Feedback" Click="MenuItem_Click">
                    <MenuItem.Icon>
                        <Image Width="20" Height="20" Source="images/feedback.ico" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>
        
        <TabControl Name="MainTabControl" Margin="0,20,0,0">
            <TabItem Header="Produkte" DataContext="{StaticResource ProductsVM}" IsSelected="{Binding IsActive, Mode=OneWayToSource}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="35" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="250" />
                    </Grid.ColumnDefinitions>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <Button Command="{Binding Add}" ToolTip="Neus Produkt erstellen" Margin="1">
                            <Image Source="Images\add.ico" />
                        </Button>
                        <Button Command="{Binding Edit}" ToolTip="Markiertes Produkt bearbeiten" Margin="1">
                            <Image Source="Images\edit.ico" />
                        </Button>
                        <Button Command="{Binding Delete}" ToolTip="Markiertes Produkt löschen" Margin="1">
                            <Image Source="Images\delete.ico" />
                        </Button>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" Grid.Column="2" HorizontalAlignment="Right">
                        <Label Target="{Binding ElementName=SearchInput}" Content="_Suche:" FontSize="18" Padding="0"/>
                        <TextBox x:Name="SearchInput" Width="150" Margin="5,0,0,0" Text="{Binding Search, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />                    </StackPanel>
                    <ListView Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding ProductsViewCollection}" Margin="5" SelectedItem="{Binding SelectedProduct, Mode=OneWayToSource}" SelectionMode="Single">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Width="40" Header="Id" DisplayMemberBinding="{Binding Id}" />
                                <GridViewColumn Width="Auto" Header="Name" DisplayMemberBinding="{Binding Name}" />
                                <GridViewColumn Width="120" Header="Preis" DisplayMemberBinding="{Binding Price, StringFormat={}{0:0.00} €}" />
                                <GridViewColumn Width="120" Header="Lagerbestand" DisplayMemberBinding="{Binding Stock}" />
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </TabItem>
            
            <TabItem Header="Kunden" Name="tabItemCustomers" DataContext="{StaticResource CustomersVM}">
                <DockPanel>
                    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                        <Button ToolTip="Neuen Kunden anlegen." Height="25" Margin="1"
                                Command="{Binding NewCommand}">
                            <Image Source="Images\add.ico"></Image>
                        </Button>
                        <Button ToolTip="Markierten Kunden löschen." Height="25" Margin="1"
                                Command="{Binding DeleteCommand}">
                            <Image Source="Images\delete.ico"></Image>
                        </Button>
                        <Button ToolTip="In die Datenbank speichern." Height="25" Margin="1"
                                Command="{Binding SaveCommand}">
                            <Image Source="Images\save.ico"></Image>
                        </Button>
                    </StackPanel>

                    <DataGrid ItemsSource="{Binding Path=Customers}" AutoGenerateColumns="False"
                              SelectionMode="Single" SelectedItem="{Binding Path=SelectedCustomer, Mode=OneWayToSource}"
                              AlternatingRowBackground="Gainsboro"  AlternationCount="2"
                              HorizontalAlignment="Left" CanUserAddRows="False" BorderBrush="#00000000" BorderThickness="0">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Id" IsReadOnly="True" Binding="{Binding Path=Id, Mode=OneWay}" Width="Auto"/>
                            <DataGridTextColumn Header="Vorname" Binding="{Binding Path=FirstName}" MinWidth="100" />
                            <DataGridTextColumn Header="Nachname" Binding="{Binding Path=LastName}" MinWidth="100" />
                            <DataGridTextColumn Header="Adresse" Binding="{Binding Path=Address}" MinWidth="200" Width="*" />
                        </DataGrid.Columns>
                    </DataGrid>
                </DockPanel>
            </TabItem>

            <TabItem Header="Bestellungen" DataContext="{StaticResource OrdersVM}">
                <TabItem.Resources>
                    <ObjectDataProvider MethodName="GetValues" x:Key="OrderStateLookup"
                                        ObjectType="{x:Type sys:Enum}">
                        <ObjectDataProvider.MethodParameters>
                            <x:Type TypeName="models:OrderState" />
                        </ObjectDataProvider.MethodParameters>
                    </ObjectDataProvider>
                    <CollectionViewSource x:Key="OrdersView"
                                          Source="{Binding Path=Orders}"/>
                    <CollectionViewSource x:Key="OrderDetailsView"
                                          Source="{Binding Source={StaticResource OrdersView}, Path='Positions'}" />
                    <CollectionViewSource x:Key="CustomerLookupView"
                                          Source="{Binding Path=CustomerLookup}"/>
                    <CollectionViewSource x:Key="ProductLookupView"
                                          Source="{Binding Path=ProductLookup}"/>
                </TabItem.Resources>
                <StackPanel Orientation="Vertical">
                    <GroupBox Header="Bestellungen">
                        <StackPanel Orientation="Vertical">
                            <StackPanel Orientation="Horizontal">
                                <Button ToolTip="Neue Bestellung anlegen." Width="25" Margin="1"
                                    Command="{Binding NewOrderCommand}">
                                    <Image Source="Images\add.ico"></Image>
                                </Button>
                                <Button ToolTip="Markierte Bestellung löschen." Width="25" Margin="1"
                                    Command="{Binding DeleteOrderCommand}">
                                    <Image Source="Images\delete.ico"></Image>
                                </Button>
                                <Button ToolTip="In die Datenbank speichern." Width="25" Margin="1"
                                    Command="{Binding SaveCommand}">
                                    <Image Source="Images\save.ico"></Image>
                                </Button>
                            </StackPanel>

                            <DataGrid ItemsSource="{Binding Source={StaticResource OrdersView}}" AutoGenerateColumns="False"
                                      SelectionMode="Single" SelectedItem="{Binding Path=SelectedOrder, Mode=OneWayToSource}"
                                      AlternatingRowBackground="Gainsboro"  AlternationCount="2"
                                      HorizontalAlignment="Left" CanUserAddRows="False" BorderBrush="#00000000" BorderThickness="0">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Id" IsReadOnly="True" Binding="{Binding Path=Id, Mode=OneWay}" />
                                    <DataGridTemplateColumn Header="Erstelldatum">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <DatePicker SelectedDate="{Binding Path=CreationTime}"></DatePicker>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                    <DataGridTemplateColumn Header="Status">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <ComboBox ItemsSource="{Binding Source={StaticResource OrdersVM}, Path=OrderStateLookup}"
                                                          SelectedItem="{Binding Path=State, UpdateSourceTrigger=PropertyChanged}" />
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                    <DataGridTemplateColumn Header="Kunde" Width="*">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <ComboBox ItemsSource="{Binding Source={StaticResource CustomerLookupView}, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
                                                          DisplayMemberPath="ToString"
                                                          IsSynchronizedWithCurrentItem="False" 
                                                          SelectedValuePath="Id"
                                                          IsReadOnly="True"
                                                          SelectedItem="{Binding Path=Customer, UpdateSourceTrigger=PropertyChanged}" />
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        </StackPanel>
                    </GroupBox>
                    
                    <GroupBox Header="Bestellungsdetails">
                        <StackPanel Orientation="Vertical">
                            <StackPanel Orientation="Horizontal">
                                <Button ToolTip="Neue Bestellungsdetails anlegen." Height="25" Margin="1"
                                    Command="{Binding NewOrderDetailCommand}">
                                    <Image Source="Images\add.ico"></Image>
                                </Button>
                                <Button ToolTip="Markierte Bestellungdetails löschen." Height="25" Margin="1"
                                    Command="{Binding DeleteOrderDetailCommand}">
                                    <Image Source="Images\delete.ico"></Image>
                                </Button>
                            </StackPanel>

                            <DataGrid ItemsSource="{Binding Source={StaticResource OrderDetailsView}, UpdateSourceTrigger=PropertyChanged}"
                                      SelectionMode="Single" AutoGenerateColumns="False"
                                      IsSynchronizedWithCurrentItem="True"
                                      SelectedItem="{Binding Path=SelectedOrderDetail, Mode=OneWayToSource}"
                                      AlternatingRowBackground="Gainsboro"  AlternationCount="2"
                                      HorizontalAlignment="Left" CanUserAddRows="False" BorderBrush="#00000000" BorderThickness="0">
                                <DataGrid.Resources>
                                    <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
                                        <Setter Property="Padding" Value="-2"/>
                                        <Style.Triggers>
                                            <Trigger Property="Validation.HasError" Value="True">
                                                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGrid.Resources>
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Id" IsReadOnly="True" Binding="{Binding Path=Id, Mode=OneWay}" />
                                    <DataGridTemplateColumn Header="Produkt">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <ComboBox ItemsSource="{Binding Source={StaticResource ProductLookupView}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                                                          DisplayMemberPath="Name"
                                                          SelectedValuePath="Id"
                                                          IsSynchronizedWithCurrentItem="False"
                                                          IsReadOnly="True"
                                                          SelectedItem="{Binding Path=Product, UpdateSourceTrigger=PropertyChanged}" />
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                    <DataGridTextColumn Header="Menge" Width="*" EditingElementStyle="{StaticResource errorStyle}" Binding="{Binding Path=Amount, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </StackPanel>
                    </GroupBox>
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
